<template>
  <div class="main user-layout-register">
    <div class="hertlist">
        {{i18n('message.firstLogin','首次登录修改密码')}}
    </div>
    <a-form ref="formRegister" :form="form" id="formRegister"  @submit="handleSubmit">

        <a-form-item>
            <a-input
              size="large"
              type="password"
              autocomplete="false"
              :placeholder="i18n('message.password','请输入密码','input')"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: i18n('message.password','请输入密码','','input')}], validateTrigger: 'blur'}
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>
           <a-form-item>
            <a-input
              size="large"
              type="password"
              autocomplete="false"
              :placeholder="i18n('message.passwordConfirm','确认密码')"
              v-decorator="[
                'password_li',
                {rules: [{ required: true, message: i18n('message.passwordConfirm','确认密码') }, { validator: this.handlePasswordCheck }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>


      <a-form-item>
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="register-button"
          :loading="registerBtn"

          :disabled="registerBtn">{{$ti18('rxForm.okBtn','确定')}}
        </a-button>
      </a-form-item>

    </a-form>
  </div>
</template>

<script>
import LoginApi from '@/api/login'
import Cookies from 'js-cookie';
import store from '@/store';
import {TokenUtil} from "@lowcode/jpaas-common-lib"

export default {
  name: 'changePassword',
  components: {
  },

  data () {
    return {
        registerBtn:false,
        //密码模式
        passwordmode: "default",
        //国产sm2公钥
        sw2PublicKey:process.env.VUE_APP_SM2_PUBLIC_KEY,
    }
  },
  computed: {
  },
    beforeCreate () {
        this.form = this.$form.createForm(this)
    },
    created() {
      this.getLoginConfig();
    },
  methods: {
      i18n(name, text, key) {
          return this.$ti18(name, text, "viewsUser", key);
      },
      handlePasswordCheck (rule, value, callback) {
        const password = this.form.getFieldValue('password')
        if (value === undefined) {
          callback(new Error(this.i18n('message.password','请输入密码','input')))
        }
        if (value && password && value.trim() !== password.trim()) {
          callback(new Error(this.i18n('tips.notMatch','两次密码不一致')))
        }
        callback()
      },
        //获取登录相关的配置参数
      getLoginConfig() {
          let self = this;
          LoginApi.getLoginConfig().then((res) => {
              let result = res.data;
              //密码加密模式
              self.passwordmode = result.passwordmode;
          });
      },
      sm2Encrypt(password) {
          const sm2 = require('sm-crypto').sm2
          let pwd= sm2.doEncrypt(password, this.sw2PublicKey);
          return pwd.toString();
      },
      handleSubmit (e) {
        e.preventDefault();
        const { form: { validateFields }, state, $router } = this ;
        let self = this;
       validateFields(['password','password_li'],{ force: true }, (err, values) => {

          if (!err) {
             let token = TokenUtil.getToken();
              if ("md5" == self.passwordmode) {
                  values.password =self.$md5(values.password.trim())
              } else if ("sm2" == self.passwordmode) {
                  values.isSm2Password="YES";
                  values.password =self.sm2Encrypt(values.password.trim())
              }
             const loginParams = {
                 token: token,
                 ...values };
              LoginApi.changePassword(loginParams).then(res => {

               if (res.code === 200) {
                   self.$message.success(res.message);
                   Cookies.remove("IdToken")
                   Cookies.remove("AccessToken")
                   Cookies.remove("Authorization")
                   Cookies.remove("XXL_JOB_LOGIN_IDENTITY")
                   TokenUtil.removeToken();
                   Cookies.remove("AUTHORIZATION_TOKEN");
                   Cookies.remove("LOGIN_TYPE");
                   store.state.appSetting.isFirstLogin = false;

                   $router.push({ name: 'login', params: { ...values } })
                   return;
               }
                  self.$message.error(res.message);

             })

          }else{

          }
        })
      },


  },

}
</script>
<style lang="less">
  .user-register {

    &.error {
      color: #ff0000;
    }

    &.warning {
      color: #ff7e05;
    }

    &.success {
      color: #52c41a;
    }

  }
  .hertlist{
    width: 100%;
    text-align:center;
    color: #555;
    font-size:20px;
    margin-bottom: 1rem;
  }
  .main{
  overflow: hidden;
  width:450px!important;
	background-color: #ffffff;
	box-shadow: 0px 0px 49px 0px
		rgba(7, 63, 126, 0.24);
  border-radius: 2px;
  padding: 20px 40px;
}
  .user-layout-register {
    .ant-input-group-addon:first-child {
      background-color: #fff;
    }
  }
    .user-layout-register {

    & > h3 {
      font-size: 16px;
      margin-bottom: 20px;
    }

    .getCaptcha {
      display: block;
      width: 100%;
      height: 40px;
    }

    .register-button {
      width: 100%;
    }

    .login {
      float: right;
      line-height: 40px;
    }
  }
</style>
